import re,timeit,random,io,struct

htmlText = "这是一串字符串<color = '#ff0000'>红色的内容结尾<item>9001,10</item></color>常规内容<item>9001,10</item>哈哈哈"
#<size=32>字号大小</size>
#通过分组 利用组别，最后一个组的正则 (</\1>) \1就是匹配第一个组(\w+) 匹配完整标签名称
p = re.compile(r"<(\w+)\s*=(.*?)>(.*?)(</\1>)+",re.DOTALL|re.IGNORECASE)


mat=re.findall(p,htmlText)
print("11=",mat)

st = timeit.default_timer()

mat=re.search(p,htmlText)
print("search group=",mat.group())

mat=re.finditer(p,htmlText)
print("finditer=",next(mat))

text = "I'm singing while you're dancing"
p = re.compile(r"\b\w+(?=ing\b)")
print(re.findall(p,text))



def genSuperLotto():
    arr1 = []
    for i in range(5):
        n = random.randint(1,35)
        while n in arr1:
            n = random.randint(1,35)
        arr1.append(n)
    arr1.sort()
    arr2 = []
    for i in range(2):
        n = random.randint(1,12)
        while n in arr2:
            n = random.randint(1,12)
        arr2.append(n)
    arr2.sort()
    arr1 = [str(i) if i>=10 else "0"+str(i) for i in arr1]
    s1 = " ".join(arr1)
    arr2 = [str(i) if i>=10 else "0"+str(i) for i in arr2]
    s2 =  " ".join(arr2)
    return s1 +" "+s2

n = 5
f = open("lotto.txt","w",encoding="utf8")
for item in range(n):
    msg = f"生成{n}个大乐透 第{item+1}个={genSuperLotto()}"
    print(msg)
    f.write(msg+"\n")
f.close()

bigNum = pow(2,64)
last = 0
for i in range(64):
    if last ==0:
        last = pow(2,i)
    elif i!=10 and i!=15:
        last = last | pow(2,i)
    if last > bigNum:
        print("last=",last,"bigNum=",bigNum,"i=",i)
print("last=",last)

byteIO = io.BytesIO()
writer = io.BufferedWriter(byteIO)

writer.write(struct.pack("<i",101))
writer.write(struct.pack("<i",102))
writer.write(struct.pack("<i",103))
writer.flush()


bytesArr1 = byteIO.getbuffer().tobytes()


writer.seek(0)

writer.write(struct.pack("<i",201))
writer.write(struct.pack("<i",202))
writer.flush()

bytesArr2 = byteIO.getvalue()

print("bytesArr1=",len(bytesArr1),"bytesArr2=",len(bytesArr2))

configPb = "data/config.bytes"
with open(configPb,"rb") as f:
    configBytes = f.read()
    head = struct.unpack_from("<i",configBytes,0)
    mask = struct.unpack_from("<i",configBytes,4)
    tableCount = struct.unpack_from("<i",configBytes,8)[0]
    
    print("head=",head,"mask=",mask,"tableCount=",tableCount)
    
    configDataDict = {}
    
    offset = 12
    for item in range(tableCount):
        nameLen = struct.unpack_from("<H",configBytes,offset)[0]
        offset += 2
        print("nameLen=",nameLen)
        nameBytes = configBytes[offset:offset+nameLen]
        name = nameBytes.decode("utf8")
        offset += nameLen
        
        
        singleTabLen = struct.unpack_from("<i",configBytes,offset)[0]
        offset += 4
        singleTabBytes = configBytes[offset:offset+singleTabLen]
        offset += singleTabLen
        
        #开始读取单个表信息
        tempOffset = 0
        rowCount = struct.unpack_from("<i",singleTabBytes,tempOffset)[0]
        tempOffset+=4
        keyLen = struct.unpack_from("<H",singleTabBytes,tempOffset)[0]
        tempOffset+=2
        keyFiled = ""
        if keyLen > 0:
            keyBytes = singleTabBytes[tempOffset:tempOffset+keyLen]
            tempOffset += keyLen
            keyFiled = keyBytes.decode("utf8")
        fieldLen = struct.unpack_from("<H",singleTabBytes,tempOffset)[0]
        tempOffset+=2
        fieldBytes = singleTabBytes[tempOffset:tempOffset+fieldLen]
        tempOffset+=fieldLen
        fieldArr = fieldBytes.decode("utf8").split(",")
        dtLen = struct.unpack_from("<H",singleTabBytes,tempOffset)[0]
        tempOffset+=2
        dtBytes = singleTabBytes[tempOffset:tempOffset+dtLen]
        tempOffset+=dtLen
        dataTypeArr = dtBytes.decode("utf8").split(",")
        print("nameLen=",nameLen,"name=",name,"rowCount=",rowCount,"keyLen=",keyLen,"keyFiled=",keyFiled,"fields=",fieldArr,"dataTypeArr=",dataTypeArr)
        rowDataArrs = [] #测试读取 统一读取到数组，实际可以根据是否有key 直接缓存到字典
        for item in range(rowCount):
            rowLen = struct.unpack_from("<i",singleTabBytes,tempOffset)[0] #当数据长度
            tempOffset += 4
            rowBytes = singleTabBytes[tempOffset:tempOffset+rowLen]
            tempOffset += rowLen
            rowInfo = {}
            rowOffset = 0
            for field,dt in zip(fieldArr,dataTypeArr):
                val = None
                if dt.startswith("int"):
                    val = struct.unpack_from("<i",rowBytes,rowOffset)[0]
                    rowOffset += 4
                elif dt.startswith("float"):
                    val = struct.unpack_from("<f",rowBytes,rowOffset)[0]
                    rowOffset += 4
                else:
                    valLen = struct.unpack_from("<i",rowBytes,rowOffset)[0]
                    rowOffset += 4
                    if valLen > 0:
                        valBytes = rowBytes[rowOffset:rowOffset+valLen]
                        val = valBytes.decode("utf8")
                    else:
                        val = ""
                    rowOffset += valLen
                rowInfo[field] = val
            rowDataArrs.append(rowInfo)
        print("rowDataArrs=",len(rowDataArrs),"rowCount=",rowCount)
        configDataDict[name] = rowDataArrs
